import 'package:flutter/material.dart';
import 'package:wu_you/res/colors.dart';
import 'package:wu_you/utils/utils.dart';

class NumberChooseWidget extends StatefulWidget {
  final int initValue;
  final int min;
  final int max;
  final Function(int value) onChange;
  final Icon reduceIcon;
  final Icon addIcon;
  NumberChooseWidget({
    Key key,
    @required this.initValue,
    this.min = 0,
    this.max,
    this.onChange,
    this.reduceIcon,
    this.addIcon
  })  : assert(initValue != null),
        super(key: key);

  @override
  _NumberChooseWidgetState createState() => _NumberChooseWidgetState();
}

class _NumberChooseWidgetState extends State<NumberChooseWidget> {
  int _number;

  @override
  void initState() {
    super.initState();
    _number = widget.initValue;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 40,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        textBaseline: TextBaseline.ideographic,
        children: [
          IconButton(
            icon: widget.reduceIcon ?? Image.asset(Utils.getImgPath("ic_sub"),width: 22,height: 22,
            fit: BoxFit.fill,color: AppColors.textHintColor,),
            splashColor: Colors.transparent,
            onPressed: () {
              if (_number > widget.min) {
                _number--;
                widget.onChange(_number);
              }
              setState(() {});
            },
          ),
          Text(
            "$_number",
            style: TextStyle(fontSize: 16, color: AppColors.textColor),
          ),
          IconButton(
            icon: widget.addIcon ?? Image.asset(Utils.getImgPath("ic_add"),width: 22,height: 22,fit: BoxFit.fill,color: AppColors.textHintColor,),
            splashColor: Colors.transparent,
            onPressed: () {
              if (widget.max != null && _number >= widget.max) {
                return;
              }
              _number++;
              widget.onChange(_number);
              setState(() {});
            },
          ),
        ],
      ),
    );
  }
}

class NumberChooseCustomWidget extends StatefulWidget {
  final int initValue;
  final int min;
  final int max;
  final Function(int value) onChange;
  final Icon reduceIcon;
  final Icon addIcon;
  const NumberChooseCustomWidget({Key key,@required this.initValue,
    this.min = 0,
    this.max,
    this.onChange,
    this.reduceIcon,
    this.addIcon}) :assert(initValue != null), super(key: key);

  @override
  NumberChooseCustomWidgetState createState() => NumberChooseCustomWidgetState();
}

class NumberChooseCustomWidgetState extends State<NumberChooseCustomWidget> {
  int _number;
  void refresh(int num){
   setState(() {
     _number = num;
   });
  }
  @override
  void initState() {
    super.initState();
    _number = widget.initValue;
  }

  @override
  Widget build(BuildContext context) {
    return Container(

      child: Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          GestureDetector(
            child: 
            
                Container(padding: EdgeInsets.all(5),child:
            widget.reduceIcon ?? Icon(Icons.remove_circle_outlined,color: AppColors.primaryColor,size: 20,)),

            onTap: () {
              if (_number > widget.min) {
                _number--;
                widget.onChange(_number);
              }
              setState(() {});
            },
          ),
          Text(
            "$_number",
            style: TextStyle(fontSize: 16, color: AppColors.textColor),
          ),
          GestureDetector(
            child:Container(padding: EdgeInsets.all(5),child: widget.addIcon ?? Icon(Icons.add_circle,color: AppColors.primaryColor,size: 20,)),
            onTap: () {
              if (widget.max != null && _number >= widget.max) {
                return;
              }
              _number++;
              widget.onChange(_number);
              setState(() {});
            },
          ),
        ],
      ),
    );
  }
}
